package com.cayden.components.kafka;


import com.cayden.config.EnvironmentHolder;
import com.cayden.config.boot.Apps;
import com.cayden.config.boot.component.ComponentInitializer;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.util.List;

/**
 * @author cayden
 * @date 2021-10-25 13:32
 */
@Slf4j
@EnableConfigurationProperties(KafkaProperty.class)
public class KafkaComponentInitializer implements ComponentInitializer {

    public KafkaComponentInitializer(){
        log.info("kafka exclude init");
    }

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext){

    }

    @Override
    public List<String> excludeAutoconfigClassNames() {
        // 对于支持跳过kafka 还是阻塞的配置
        Environment environment = EnvironmentHolder.get();
        String property = environment.getProperty(KafkaProperty.PERFIX + ".supportMultiAutoConfiguration");
        // 不管是不是那啥,都要去检测是不是能连上

        if (StringUtils.isBlank(property) || !property.equals("true")){
            KafkaHasConnectionUtils.hasConnection();
            return Lists.newArrayList();
        }

        if (KafkaHasConnectionUtils.hasConnection()){
            return Lists.newArrayList();
        }
        return Lists.newArrayList("org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration");
    }

}
